package com.wyl.mybatis.demo;

import com.wyl.mybatis.config.SqlSessionFactoryConfig;
import com.wyl.mybatis.entity.FullCity;
import com.wyl.mybatis.mapper.FullCityMapper;
import com.wyl.mybatis.req.FullCityReq;
import org.apache.ibatis.session.ExecutorType;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.IOException;
import java.util.List;

/**
 * @Description
 * @Author WuYiLong
 * @Date 2024/3/14 11:41
 */
public class SimpleDemo {

    private final Logger log = LoggerFactory.getLogger(SimpleDemo.class);

    public static void main(String[] args) throws IOException {
        // 获取当前的数据库名称和版本号
        SqlSessionFactory sqlSessionFactory = SqlSessionFactoryConfig.buildSqlSessionFactory();
        // SqlSessionFactory 通过解析配置文件（通常是 mybatis-config.xml）和映射文件（mapper.xml），来创建一个能够执行映射语句的会话对象SqlSession
        try (SqlSession sqlSession = sqlSessionFactory.openSession(ExecutorType.SIMPLE)) {
            FullCityMapper mapper = sqlSession.getMapper(FullCityMapper.class);

            // 使用SqlSession获取映射器实例
            // 使用映射器执行操作
            FullCity fullCity = mapper.selectByName("广东省");
            FullCity fullCity1 = mapper.selectByNameAndCode("广东省", "44");
            FullCityReq fullCityReq = new FullCityReq();
            fullCityReq.setName("广东省");
            FullCity fullCity2 = mapper.selectFullCity(fullCityReq);
            List<FullCity> fullCities = mapper.selectFullCityLikeName("东");
            System.out.println("城市的名称：" + fullCity.getName());
        }
    }
}
